package com.kzy.demoadmin.web.controller;

import com.github.pagehelper.PageInfo;
import com.kzy.demoadmin.web.entity.Log;
import com.kzy.demoadmin.web.entity.Notice;
import com.kzy.demoadmin.web.entity.Result;
import com.kzy.demoadmin.web.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 公告管理
 */
@RestController
@RequestMapping("notices")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 保存公告
     *
     * @param notice
     * @return
     */
    @PostMapping("/save")
    public Result add(@RequestBody Notice notice) {
        if (notice.getId() == null) {
            noticeService.add(notice);
        } else {
            noticeService.edit(notice);
        }
        return Result.success();
    }


    /**
     * 分页查询+模糊查询
     *
     * @param notice
     * @return
     */
    @GetMapping("/list")
    public Result getNoticeList(Notice notice) {
        // 调用服务层分页查询
        PageInfo<Notice> info = noticeService.getNoticeList(notice);
        return Result.success(info);
    }

    /**
     * 根据id删除公告
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(noticeService.deleteById(id));
    }

    /**
     * 根据id查询公告
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        //待完善
        return Result.success(noticeService.findById(id));
    }

}
